<?php
/**
 * @since      File available since Release v1.1
 */
defined('Inbearshop') or exit('Access Invalid!');
define('MYSQL_RESULT_TYPE',1);
class orderControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
	}

	public function orderuserdetailOp(){
		
		$order_id = intval($_GET['order_id']);
		$model = Model();
		$order = $model->table('order')->where(array('id'=>$order_id))->find();		
		$user = $model->table('user')->where(array('user_id'=>$order['seller_id']))->find();
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		
		if($order['user_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		Tpl::output('order',$order);
		Tpl::output('user',$user);
		Tpl::output('auction',$auction);
		
		$auction_log = $model->table('auction_log')->where(array('auction_id'=>$auction['id']))->order('id asc')->select();
		Tpl::output('auction_log',$auction_log[0]);
		
		$address = array();
		if($order['order_state']>1){
			$address = $model->table('address')->where(array('id'=>$order['order_address']))->find();

			$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
			$address['prov_name'] = $prov['region_name'];
			
			$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
			$address['city_name'] = $city['region_name'];
			
			if(!empty($address['did'])){
				$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
				$address['dis_name'] = $dis['region_name'];
			}else{
				$address['dis_name'] = '';
			}
		}

		Tpl::output('address',$address);
		
		if(!empty($order['tuihuo_address'])){
			$tuihuo_address = $model->table('address')->where(array('id'=>$order['tuihuo_address']))->find();
			$prov = $model->table('region')->where(array('region_code'=>$tuihuo_address['pid']))->find();
			$tuihuo_address['prov_name'] = $prov['region_name'];
				
			$city = $model->table('region')->where(array('region_code'=>$tuihuo_address['cid']))->find();
			$tuihuo_address['city_name'] = $city['region_name'];
				
			if(!empty($address['did'])){
				$dis = $model->table('region')->where(array('region_code'=>$tuihuo_address['did']))->find();
				$tuihuo_address['dis_name'] = $dis['region_name'];
			}else{
				$tuihuo_address['dis_name'] = '';
			}
		}
		Tpl::output('tuihuo_address',$tuihuo_address);
		
		if($order['is_tuihuo']==1){
			$refund = $model->table('refund')->where(array('order_id'=>$order['id']))->order('refund_add_time desc')->select();
			Tpl::output('refund',$refund);
		}
		
		$platform = $model->table('platform')->where(array('order_id'=>$order['id']))->find();
		Tpl::output('platform',$platform);
		
		Tpl::showpage('order.userdetail');		
	}
	
	
	public function ordersellerdetailOp(){
		$auction_id = intval($_GET['auction_id']);
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>$auction_id))->find();
		$user = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
		
		
		if(empty($auction)){
			$this->showTip('该拍品已下架','index.php?act=index');
		}
		
		if($auction['is_liupai']==2){
			Tpl::output('user',$user);
			Tpl::output('auction',$auction);
			Tpl::showpage('order.sellerliupai');
			exit;
		}

		$order = $model->table('order')->where(array('order_sn'=>$auction['auction_order_sn']))->find();
		
		if($order['seller_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		Tpl::output('order',$order);
		
		Tpl::output('user',$user);
		Tpl::output('auction',$auction);
		
		$userinfo = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
		Tpl::output('userinfo',$userinfo);
		
		$auction_log = $model->table('auction_log')->where(array('auction_id'=>$auction['id']))->order('id asc')->select();
		Tpl::output('auction_log',$auction_log[0]);

		$address = array();
		$address = $model->table('address')->where(array('id'=>$order['order_address']))->find();
		$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
		$address['prov_name'] = $prov['region_name'];
			
		$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
		$address['city_name'] = $city['region_name'];
			
		if(!empty($address['did'])){
			$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
			$address['dis_name'] = $dis['region_name'];
		}else{
			$address['dis_name'] = '';
		}
		Tpl::output('address',$address);
		
		if(!empty($order['tuihuo_address'])){
			$tuihuo_address = $model->table('address')->where(array('id'=>$order['tuihuo_address']))->find();
			$prov = $model->table('region')->where(array('region_code'=>$tuihuo_address['pid']))->find();
			$tuihuo_address['prov_name'] = $prov['region_name'];
			
			$city = $model->table('region')->where(array('region_code'=>$tuihuo_address['cid']))->find();
			$tuihuo_address['city_name'] = $city['region_name'];
			
			if(!empty($address['did'])){
				$dis = $model->table('region')->where(array('region_code'=>$tuihuo_address['did']))->find();
				$tuihuo_address['dis_name'] = $dis['region_name'];
			}else{
				$tuihuo_address['dis_name'] = '';
			}
		}
		Tpl::output('tuihuo_address',$tuihuo_address);
		
		if($order['is_tuihuo']==1){
			$refund = $model->table('refund')->where(array('order_id'=>$order['id']))->order('refund_add_time desc')->select();
			Tpl::output('refund',$refund);
		}		
		
		Tpl::showpage('order.sellerdetail');
	}
	
	
	public function delayOp(){
		$condition = array();
		$condition['id'] = $_GET['order_id'];
		$condition['user_id'] = $_COOKIE['user_id'];

		$model = Model();
		$res = $model->table('order')->where($condition)->update(array('is_delay'=>'1'));

		if($res){
			echo json_encode(array('state'=>1));
		}else{
			echo json_encode(array('state'=>0));
		}
		exit;
	}
	
	/*
	 * 买家退货
	 */
	public function lijituihuoOp(){
		
		if(isset($_POST)&&!empty($_POST)){

			$params = array();
			$params['tuihuo_express'] = $_POST['com'];
			$params['tuihuo_expresscode'] = $_POST['code'];
			$params['tuihuo_expresstime'] = time();
			
			$condition = array();
			$condition['id'] = $_POST['order_id'];
			
			$model = Model();
			$res = $model->table('order')->where($condition)->update($params);
			if($res){
				$order = $model->table('order')->where(array('id'=>$_POST['order_id']))->find();
				$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
				$seller = $model->table('user')->where(array('user_id'=>$order['seller_id']))->find();
				
				$data = array();
				$data['touser'] = $seller['openid'];
				$data['template_id'] = $this->getTemplateId(1);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
				$data['data']['first']['value'] = "尊敬的{$seller['nickname']},买家申请的退货的拍品已发货";
				$data['data']['keyword1']['value'] = $_POST['com'];
				$data['data']['keyword2']['value'] = $_POST['code'];
				$data['data']['keyword3']['value'] = $auction['auction_description'];
				$data['data']['keyword4']['value'] = '1';
				$data['data']['remark']['value'] = '提醒：请您在收到退货后，再退款给买家（7天后，将自动确认退款给买家）';
				$data1 = json_encode($data);
				$this->send_template_message($data1);
				
				echo json_encode(array('state'=>1));
			}
			exit;
		}
		
		$condition = array();
		$condition['id'] = intval($_GET['order_id']);
		$model = Model();
		$order = $model->table('order')->where($condition)->find();
		Tpl::output('order',$order);
		
		if($order['user_id']!=$_COOKIE['user_id']){
			$this->showTip('您访问的地址不存在','index.php?act=index');
			exit;
		}
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		Tpl::output('auction',$auction);
		
		$address = $model->table('address')->where(array('id'=>$order['tuihuo_address']))->find();
		$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
		$address['prov_name'] = $prov['region_name'];
		
		$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
		$address['city_name'] = $city['region_name'];
		
		if(!empty($address['did'])){
			$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
			$address['dis_name'] = $dis['region_name'];
		}else{
			$address['dis_name'] = '';
		}
		Tpl::output('address',$address);
		
		Tpl::showpage('order.lijituihuo');
	}
	
	public function currentexchangeOp(){
		
		$condition = array();
		$condition['id'] = intval($_GET['order_id']);;
		$model = Model();
		$order = $model->table('order')->where($condition)->find();
		if(empty($order)){
			echo json_encode(array('state'=>'0'));
			exit;
		}
		
		$params = array();
		$params['order_state'] = 2;
		$params['currentexchange'] = 1;
		$params['fahuo_time'] = time();
		$model = Model();
		$res = $model->table('order')->where($condition)->update($params);
		if($res){
			$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();
			$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
			
			$data = array();
			$data['touser'] = $user['openid'];
			$data['template_id'] = $this->getTemplateId(1);
			$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
			$data['data']['first']['value'] = "尊敬的{$user['nickname']},您的拍品已经发货";
			$data['data']['keyword1']['value'] = '当面交易';
			$data['data']['keyword2']['value'] = '-';
			$data['data']['keyword3']['value'] = $auction['auction_description'];
			$data['data']['keyword4']['value'] = '1';
			$data['data']['remark']['value'] = '提醒：收到拍品后，请及时收货!（发货7天后自动确认收货）';
			$data1 = json_encode($data);
			$t = $this->send_template_message($data1);

			echo json_encode(array('state'=>1));
			exit;
		}	
	}
	
	
	public function refusesendoutOp(){
		
		$condition = array();
		$condition['id'] = intval($_GET['order_id']);
		
		$params = array();
		$params['is_fail'] = 1;
		
		$model = Model();
		$res = $model->table('order')->where($condition)->update($params);
		if($res){
			echo json_encode(array('state'=>1));
		}
		exit;
		
	}
	
	public function platdealOp(){
		
		if(isset($_POST)&&!empty($_POST)){
			
			$params = array();
			$params['order_id'] = intval($_POST['order_id']);
			$params['deal_description'] = trim($_POST['deal_description']);
			$params['deal_add_time'] = time();
			
			$model = Model();
			$res = $model->table('platform')->insert($params);
			if($res){
				echo json_encode(array('state'=>1));				
			}
			exit;
		}
		
		$model = Model();
		$order = $model->table('order')->where(array('id'=>intval($_GET['order_id'])))->find();
		Tpl::output('order',$order);
		
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		Tpl::output('auction',$auction);
		
		$user = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
		Tpl::output('user',$user);
		
		
		
		Tpl::showpage('order.platdeal');		
	}	
	
}